package com.neusoft.lohas.common.restTemplate.client;

import java.util.HashMap;
import java.util.Map;

import com.neusoft.lohas.common.utils.StatusBean;

/**
 * 用于封装有状态值的处理结果，一般有确定返回值、不需要确切记录状态值的不要用他
 * @author lichap
 *
 */
public class HandResultBean<T> {

	public static final HandResultBean EMTY_RESULT = new HandResultBean(
			HandleStatusEnum.SUCCESS);

	public static <T> HandResultBean<T> buildEmtyResultBean() {
		return EMTY_RESULT;
	}

	int status = HandleStatusEnum.SUCCESS.getStatusCode();
	HandleStatusEnum statusEnum = HandleStatusEnum.SUCCESS;
	/**
	 * message要考虑国际化，所以这里的值不能再action层意外的地方做
	 */
	String message;
	T targetObject;

	Map<String, Object> parameters = new HashMap<String, Object>(3);

	public HandResultBean() {
	}

	public HandResultBean(HandleStatusEnum status) {
		this.status = status.getStatusCode();
		this.statusEnum = status;
	}

	public HandResultBean(int status, T target)
	{
		this.status = status;
		this.targetObject = target;
		this.statusEnum = HandleStatusEnum.createFromCode(status);
	}

	public HandResultBean(HandleStatusEnum status, T target)
	{
		this.status = status.getStatusCode();
		this.statusEnum = status;
		this.targetObject = target;
	}

	public static <T> HandResultBean<T> error() {
		return new HandResultBean<T>(ResponseCode.COMMON_ERROR,
				"服务器错误稍后重试");
	}

	public static <T> HandResultBean<T> error(String message) {
		return new HandResultBean<T>(ResponseCode.COMMON_ERROR,
				message);
	}

	public static <T> HandResultBean<T> FORBIDDEN() {
		return new HandResultBean<T>(ResponseCode.FORBIDDEN,
				"权限不足");
	}

	public static <T> HandResultBean<T> FORBIDDEN(String message) {
		return new HandResultBean<T>(ResponseCode.FORBIDDEN,
				message);
	}

	public static HandResultBean<Void> assertBigThan(int value, int base) {
		if (value > base)
			return new HandResultBean<Void>(ResponseCode.SUCCESS, "");
		return new HandResultBean<Void>(ResponseCode.COMMON_ERROR,
				"服务器错误，稍后重试");
	}

	public static HandResultBean<Void> assertBigThan(int value, int base,
			String message) {
		if (value > base)
			return new HandResultBean<Void>(ResponseCode.SUCCESS, "");
		return new HandResultBean<Void>(ResponseCode.COMMON_ERROR,
				message);
	}

	public static <T> HandResultBean<T> assertNotNull(T value,
			HandleStatusEnum status, String errorMessage) {
		if (value == null) {
			return new HandResultBean<T>(status, errorMessage);
		}
		return new HandResultBean<T>(HandleStatusEnum.SUCCESS, value);
	}

	public HandResultBean(HandleStatusEnum status, String message)
	{
		System.out.println("优先调用这里");
		this.status = status.getStatusCode();
		this.statusEnum = status;
		this.message = message;
	}

	public HandResultBean(int status, String message) {
		this.message = message;
		this.status = status;
		this.statusEnum = HandleStatusEnum.createFromCode(status);
	}

	public HandResultBean(int status, String message, T taget)
	{
		this.message = message;
		this.status = status;
		this.targetObject = taget;
	}

	public int getStatus() {

		return status;
	}

	/**
	 * 用setStatusEnum
	 * @param status
	 */
	@Deprecated
	public void setStatus(int status) {
		this.status = status;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Map<String, Object> getParameters() {
		return parameters;
	}

	public void setParameters(Map<String, Object> parameters) {
		this.parameters = parameters;
	}

	public Object getParameter(String key)
	{
		return parameters.get(key);
	}

	public Object removeParemeter(String key)
	{
		return parameters.remove(key);
	}

	public Object addParameter(String key, Object value)
	{
		return parameters.put(key, value);
	}

	public T getTargetObject() {
		return targetObject;
	}

	public HandResultBean<T> setTargetObject(T targetObject) {
		this.targetObject = targetObject;
		return this;
	}

	public final HandleStatusEnum getStatusEnum() {
		return statusEnum;
	}

	public final void setStatusEnum(HandleStatusEnum statusEnum) {
		this.statusEnum = statusEnum;
		this.status = statusEnum.getStatusCode();
	}

	public final void setStatusEnum(HandleStatusEnum statusEnum, String message) {
		this.statusEnum = statusEnum;
		this.status = statusEnum.getStatusCode();
		this.message = message;
	}

	public StatusBean createStatusBean() {
		return new StatusBean(statusEnum.getStatusCode(), message);
	}

	public boolean haveError() {
		return (status / 100) > 2;
	}

	@Override
	public String toString()
	{
		return "{\"status\":" + status + ",\"message\":\"" + message + "\"}";
	}

}
